//
//  KABCosplayTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/3.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosplayTCell.h"
#import "KABModel.h"
#import <YYWebImage.h>
#import "NSString+XDNSDate.h"
#import "TTTAttributedLabel.h"
#import "GlobalHeader.h"
#import "UIColor+XDHex.h"
#import "NSString+XDQiniu.h"

@interface KABCosplayTCell ()<TTTAttributedLabelDelegate>

@end

@implementation KABCosplayTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.portraitImgView.layer.masksToBounds = YES;
    self.portraitImgView.layer.cornerRadius = CGRectGetWidth(self.portraitImgView.frame) / 2.0;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(portraitImgViewTapped:)];
    [self.portraitImgView addGestureRecognizer:tapGesture];
    
    [self.pullDownBtn addTarget:self action:@selector(pullDownBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.praiseBtn addTarget:self action:@selector(praiseBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.commentBtn addTarget:self action:@selector(commentBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.collectBtn addTarget:self action:@selector(collectBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.deleteBtn addTarget:self action:@selector(deleteBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    NSDictionary *atts = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorTabBarItemColor]};
    self.contentLabel.linkAttributes = atts;
    self.contentLabel.activeLinkAttributes = nil;
    self.contentLabel.inactiveLinkAttributes = nil;
    self.contentLabel.delegate = self;
//    self.contentLabel.font = [UIFont systemFontOfSize:14.0];
//    self.contentLabel.textColor = [UIColor xd_colorWithHex:0x151515];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)portraitImgViewTapped:(UIGestureRecognizer *)gesture {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:portraitImgViewTapped:)]) {
        [self.delegate kABCosplayTCell:self portraitImgViewTapped:(UIImageView *)gesture.view];
    }
}

- (void)pullDownBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:pullDownBtnTapped:)]) {
        [self.delegate kABCosplayTCell:self pullDownBtnTapped:btn];
    }
}

- (void)praiseBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:praiseBtnTapped:)]) {
        [self.delegate kABCosplayTCell:self praiseBtnTapped:btn];
    }
}

- (void)commentBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:commentBtnTapped:)]) {
        [self.delegate kABCosplayTCell:self commentBtnTapped:btn];
    }
}

- (void)collectBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:collectBtnTapped:)]) {
        [self.delegate kABCosplayTCell:self collectBtnTapped:btn];
    }
}

- (void)deleteBtnTapped:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:deleteBtnTapped:)]) {
        [self.delegate kABCosplayTCell:self deleteBtnTapped:btn];
    }
}

#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([self.delegate respondsToSelector:@selector(kABCosplayTCell:linkTapped:)]) {
        [self.delegate kABCosplayTCell:self linkTapped:url];
    }
}

- (void)configViewWithCosplayItem:(KABCosListItem *)item showDeleteBtn:(BOOL)show showPullDownBtn:(BOOL)showPull {
    [self.portraitImgView yy_setImageWithURL:[NSURL URLWithString:item.publisher_imageurl]
                                 placeholder:[KABDefaultVarModel sharedInstance].smallDefaultImg
                                     options:YYWebImageOptionSetImageWithFadeAnimation
                                  completion:nil];
    
    self.nameLabel.text = item.publisher;
    self.timeLabel.text = [item.publish_time xd_yyyyMMddHHmmssStrToFriendDateStr];
    self.locationLabel.text = (item.publish_address.length ? item.publish_address : @"未知");
    self.activityImgView.hidden = !item.is_competition;
    self.contentLabel.text = item.topContentLinkAStr;
    if (!item.top_content.length) {
        self.contentLabelTopConstraint.constant = 0;
        self.contentLabelBottomContraint.constant = 0;
    } else {
        self.contentLabelTopConstraint.constant = 14.0;
        self.contentLabelBottomContraint.constant = 14.0;
    }
    
    NSString *thumbUrl = [item.image_url xd_qiniuImgUrlWithMaxWidth:CGRectGetWidth([UIScreen mainScreen].bounds)];
    NSLog(@"缩略图的url = %@", thumbUrl);
    [self.contentImgView yy_setImageWithURL:[NSURL URLWithString:thumbUrl]
                                placeholder:[KABDefaultVarModel sharedInstance].bigDefaultImg
                                    options:YYWebImageOptionSetImageWithFadeAnimation
                                 completion:nil];
    
    self.praiseBtn.selected = item.is_praise;
    [self.praiseBtn setTitle:[NSString stringWithFormat:@" %zd ", item.praise] forState:UIControlStateNormal];
    [self.commentBtn setTitle:[NSString stringWithFormat:@" %zd ", item.total_comment] forState:UIControlStateNormal];
    [self.collectBtn setTitle:@" 收藏" forState:UIControlStateNormal];
    [self.collectBtn setTitle:@" 收藏" forState:UIControlStateSelected];
    self.collectBtn.selected = item.is_collected;
    
    self.deleteBtn.hidden = !show;
    self.collectBtn.hidden = show;
    
    self.pullDownBtn.hidden = !showPull;
}

@end
